import { useI18n } from 'vue-i18n'
import { ProposalType, getAllDimensionLevelKeys, getNotImprovedMap } from '@/controller/Dishwasher/Learning/constants'
import type { ApplianceStatus } from '@/api/appliance'
import i18n from '@/i18n'
import type { DishwasherController } from '@/controller/Dishwasher/definition'

/**
 * Learning 视图显示场景
 */
export enum LearningViewScene {
  /**
   * 详情页
   */
  Detail,
  /**
   * 评价反馈
   */
  Feedback,
}

/**
 * Learning 视图打开入口
 */
export enum LearningViewEntry {
  /**
   * 从 FlexArea 打开
   */
  FlexArea,
  /**
   * 从 程序启动栏 打开
   */
  ActionBar,
}

/**
 * Learning Proposal 选项
 */

// 例: { Current: '之前方案', Main: '优化方案', ... }
type ProposalMap = Record<ProposalType, string>
// 全量的 Proposal 选项Map
export function useFullProposalMap(payload: { isPerfectProposal: ComputedRef<boolean> }) {
  const i18n = useI18n()
  return computed<ProposalMap>(() => {
    // 判断是否显示 满意方案
    const currentProposalName = payload.isPerfectProposal.value
      ? getPerfectProposalName()
      : i18n.t('Appliance.LearningProgram.Proposal.Current.Text')

    return {
      [ProposalType.Current]: currentProposalName,
      [ProposalType.Main]: i18n.t('Appliance.LearningProgram.Proposal.Main.Text'),
      [ProposalType.Speed]: i18n.t('Appliance.LearningProgram.Proposal.Speed.Text'),
      [ProposalType.Eco]: i18n.t('Appliance.LearningProgram.Proposal.Eco.Text'),
    }
  })
}

export type ProposalOption = {
  type: ProposalType
  label: string
}
// 可用的 Proposal 选项
export function useValidProposalOptions(payload: { allStatus: ComputedRef<ApplianceStatus[]> }) {
  const i18n = useI18n()
  // Proposal 键值对映射
  const fullProposalMap = useFullProposalMap({
    ...payload,
    isPerfectProposal: computed(() => false), // 先给默认值 防止循环引用
  })
  // 获取选项列表
  const proposalOptions = computed<ProposalOption[]>(() => {
    const keyValuePairs = Object.entries(fullProposalMap.value) as [ProposalType, string][]
    // 根据键值对 生成可选择的 Proposal 选项列表
    const options = keyValuePairs.reduce<ProposalOption[]>((validOptions, proposalConfig) => {
      const [type, label] = proposalConfig
      if (type !== ProposalType.Current && hasProposal(type)) {
        validOptions.push({ type, label })
      }
      return validOptions
    }, [])

    return options
  })

  // 判断Status中是否存在3种Level的Proposal (Current默认存在 所以不适用此方法)
  function hasProposal(type: Exclude<ProposalType, ProposalType.Current>): boolean {
    const allStatus = payload.allStatus.value
    const levelKeys = getAllDimensionLevelKeys(type)
    return levelKeys.every(({ key }) => allStatus.some((status) => status.key === key))
  }

  return proposalOptions
}

/**
 * 完美方案判断
 */
export function getPerfectProposalName() {
  return i18n.global.t('Appliance.LearningProgram.Proposal.Current.Perfect.Text')
}
// 除Current外 无方案选项 为完美方案
export function checkPerfectProposal(options: ProposalOption[]) {
  return options.length === 0
}

/**
 * 无法改善提示
 */
export function useCanNotBeImprovedInfo(payload: {
  control: DishwasherController.Control
  selected: Ref<ProposalType | null> | ComputedRef<ProposalType | null>
}): ComputedRef<string | null> {
  const { control, selected } = payload
  return computed(() => {
    if (!selected.value) return null

    const notImprovedMap = getNotImprovedMap(selected.value)
    const warningLabels = control.store._datasource.status.reduce<string[]>((labels, status) => {
      const matchedInfo = notImprovedMap.find((item) => item.key === status.key && status.value === true)
      if (matchedInfo) {
        labels.push(matchedInfo.label)
      }
      return labels
    }, [])

    if (warningLabels.length !== 0) {
      const splitter = i18n.global.t('Appliance.LearningProgram.ProposalAdjust.Modal.NotImproved.LabelSplitter.Text')
      const labelText = warningLabels.join(splitter)
      return i18n.global
        .t('Appliance.LearningProgram.ProposalAdjust.Modal.NotImproved.Content')
        .replace('{label}', labelText)
    }

    return null
  })
}
